package com.sojson.project.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.constant.ConstantCache;
import com.sojson.project.dao.DepositDao;
import com.sojson.project.entity.po.Deposit;
import com.sojson.project.service.DepositService;
import com.sojson.util.ArithUtil;
import com.sojson.util.DateUtil;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.cache.CacheUtil;

/**
 * 诚意金信息表Service实现类
 * 
 * @author liu
 * @date 2021-05-08
 */
@Service
public class DepositServiceImpl implements DepositService {

    @Resource
    private DepositDao depositDaoImpl;

    /**
     * 单条插入
     * 
     * @param deposit
     * @return
     */
    @Override
    public int insert(Deposit deposit) {
        int insert = depositDaoImpl.insert(deposit);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param deposit
     * @return
     */
    @Override
    public int insertList(Deposit deposit) {
        int insertList = depositDaoImpl.insertList(deposit.getDeposits());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int deleteByDto(Deposit deposit) {
        int deleteByDto = depositDaoImpl.deleteByDto(deposit);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @Override
    public int countAll() {
        int countAll = depositDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @Override
    public List<Deposit> findAll() {
        List<Deposit> findAll = depositDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param deposit
     * @return
     */
    @Override
    public int countByDto(Deposit deposit) {
        int countByDto = depositDaoImpl.countByDto(deposit);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param deposit
     * @return
     */
    @Override
    public List<Deposit> findByDto(Deposit deposit) {
        List<Deposit> findByDto = depositDaoImpl.listByDto(deposit);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = depositDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int deleteByIdList(List<Integer> ids) {
        int deleteByIdList = depositDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updateById(Deposit deposit) {
        int updateById = depositDaoImpl.updateById(deposit);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updateStaticByIdList(Deposit deposit) {
        int updateStaticByIdList = depositDaoImpl.updateStaticByIdList(deposit);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updateChangeByIdList(Deposit deposit) {
        int updateChangeByIdList = depositDaoImpl.updateChangeByIdList(deposit.getDeposits());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updatePartialById(Deposit deposit) {
        int updatePartialById = depositDaoImpl.updatePartialById(deposit);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updateStaticPartialByIdList(Deposit deposit) {
        int updateStaticPartialByIdList = depositDaoImpl.updateStaticPartialByIdList(deposit);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param deposit
     * @return
     */
    @Override
    public int updateChangePartialByIdList(Deposit deposit) {
        int updateChangePartialByIdList = depositDaoImpl.updateChangePartialByIdList(deposit.getDeposits());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public Deposit findById(Integer id) {
        Deposit findById = depositDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    @Override
    public List<Deposit> findByIdList(List<Integer> ids) {
        List<Deposit> findByIdList = depositDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param deposit
     * @return
     */
    @Override
    public List<Integer> findIdByDto(Deposit deposit) {
        List<Integer> findIdByDto = depositDaoImpl.findIdByDto(deposit);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public void clear() {
        depositDaoImpl.clear();
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws IOException 
     */
    @Override
    public Deposit getInfo() throws IOException {
        Deposit deposit = CacheUtil.get(ConstantCache.DEPOSIT_KEY);
        if (StringUtil.isNotBlankObject(deposit)) {
            return deposit;
        }

        Deposit findById = depositDaoImpl.findById(1);
        CacheUtil.set(ConstantCache.DEPOSIT_KEY, findById);
        return findById;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param deposit
     * @return
     * @throws IOException 
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int update(Deposit deposit) throws IOException {
        deposit.setId("1");
        BigDecimal total = deposit.getTotal();
        if (StringUtil.isNotBlankObject(total)) {
            int intValue = total.intValue();
            if (intValue <= 0) {
                ExpUtil.throwEx("诚意金必须大于0！");
            }
            deposit.setTotal(ArithUtil.intPennyToBigDollar(intValue));
        }
        deposit.setUpdatedAt(DateUtil.nowDate());

        int updatePartialById = depositDaoImpl.updatePartialById(deposit);
        Deposit findById = depositDaoImpl.findById(1);
        CacheUtil.set(ConstantCache.DEPOSIT_KEY, findById);
        return updatePartialById;
    }

}